<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'大夫转化率'),
);
//客户列表
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.$_RQ['name'].'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$memberparams=array('uniacid'=>$_SESSION['uniacid']);
	$yueparams=$memberparams;
	$yueparams['ispay']=1;
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		//$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	if(!$between){
		$memberparams['createtime >']=0;
		$yueparams['createtime >']=0;
	}else{
		$memberparams['createtime between']=$between;
		$yueparams['createtime between']=$between;
	}
	$list=pdo_getslice('jy_yiliao_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		foreach($list as &$item){
			$memberparams['fromworkerid']=$yueparams['worker_id']=$item['id'];
			$item['memberall']=pdo_count('jy_yiliao_user',$memberparams);
			$item['yuenum']=pdo_count('jy_yiliao_order',$yueparams);
			$item['yuefee']=pdo_getsum('jy_yiliao_order',$yueparams,'pay');
			$item['yes']=pdo_getsum('jy_yiliao_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'createtime between'=>array($yesyue,$nowyue)),'pay');
		}
	}
	$heji=array('title'=>'合计');
	paixu($list,'yuefee','desc');
	foreach($list as $k=>&$v){
		$v['rank']=$k+1;
		$heji['memberall'] +=$v['memberall'];
		$heji['yuenum'] +=$v['yuenum'];
		$heji['yuefee'] +=$v['yuefee'];
		$heji['yes'] +=$v['yes'];
	}
	$list[]=$heji;
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'rank','title'=>'排名','type'=>'string'),
			array('name'=>'name','title'=>'大夫姓名','type'=>'string'),
			array('name'=>'memberall','title'=>'渠道客户数','type'=>'string'),
			array('name'=>'yuenum','title'=>'问诊数','type'=>'string'),
			array('name'=>'yuefee','title'=>'问诊费','type'=>'string'),
			array('name'=>'yes','title'=>'上月业绩','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'大夫姓名','name'=>'name','help'=>''),
			array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.$_RQ['name'].'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$memberparams=array('uniacid'=>$_SESSION['uniacid']);
	$yueparams=$memberparams;
	$yueparams['ispay']=1;
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		//$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	if(!$between){
		$memberparams['createtime >']=0;
		$yueparams['createtime >']=0;
	}else{
		$memberparams['createtime between']=$between;
		$yueparams['createtime between']=$between;
	}
	$list=pdo_getall('jy_yiliao_worker',$params,$fields,'','id desc');
	if($list){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		foreach($list as &$item){
			$memberparams['fromworkerid']=$yueparams['worker_id']=$item['id'];
			$item['memberall']=pdo_count('jy_yiliao_user',$memberparams);
			$item['yuenum']=pdo_count('jy_yiliao_order',$yueparams);
			$item['yuefee']=pdo_getsum('jy_yiliao_order',$yueparams,'pay');
			$item['yes']=pdo_getsum('jy_yiliao_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'createtime between'=>array($yesyue,$nowyue)),'pay');
		}
	}
	$heji=array('rank'=>'合计');
	paixu($list,'yuefee','desc');
	foreach($list as $k=>&$v){
		$v['rank']=$k+1;
		$heji['memberall'] +=$v['memberall'];
		$heji['yuenum'] +=$v['yuenum'];
		$heji['yuefee'] +=$v['yuefee'];
		$heji['yes'] +=$v['yes'];
	}
	$list[]=$heji;
	$header=array(
		'rank'=>'排名',
		'title'=>'大夫姓名',
		'memberall'=>'渠道客户数',
		'yuenum'=>'问诊数',
		'yuefee'=>'问诊费',
		'yes'=>'上月业绩',
	);
	model('core/tools')->export2excel($header,$list,'大夫数据');
}